const managerDao = require('../dao/manager');
const powerService = require('./power');

const managerService = {
    // 获取所有副卡密列表
    async getAllSubCardLazyLoad(uid, userTypeId, cursor, limit) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看副卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllSubCardLazyLoad(uid, cursor, limit);
            if (result.length > limit) {
                let nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor } };
            } else {
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor: null } };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 获取所有副卡密列表
    async getAllSubCard(uid, userTypeId) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看副卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllSubCard(uid);
            result = { code: 200, msg: "获取成功", data: result };
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 删除副卡密
    async delSubCard(uid, userTypeId, cardId) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "删除副卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.delSubCard(uid, cardId);
            if (result.changedRows > 0) {
                result = { code: 200, msg: "删除成功", data: null };
            } else {
                result = { code: 500, msg: "删除失败", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 修改副卡密信息
    async updateSubCard(uid, userTypeId, cardId, name, url) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "修改副卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 获取用户类型
            result = await powerService.getUserType(userTypeId);
            if (!result.data) {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
            let userType = result.data.name;
            if (userType == "主卡密用户") {
                // 副卡密判空
                if (!cardId) {
                    return res.status(400).json({
                        code: 400,
                        msg: "卡密ID不允许为空",
                        data: null
                    });
                }
                // 调用数据层
                result = await managerDao.updateSubCard(cardId, name, url);
                if (result.changedRows > 0) {
                    result = { code: 200, msg: "修改成功", data: null };
                } else {
                    result = { code: 500, msg: "数据无变化", data: null };
                }
            } else if (userType == "副卡密用户") {
                // 调用数据层
                result = await managerDao.updateSubCard(uid, name, url);
                if (result.changedRows > 0) {
                    result = { code: 200, msg: "修改成功", data: null };
                } else {
                    result = { code: 500, msg: "数据无变化", data: null };
                }
            } else {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 获取副卡密详情
    async getSubCardInfo(uid, userTypeId, cardId) {
        let result;
        try {
            // 获取用户类型
            result = await powerService.getUserType(userTypeId);
            if (!result.data) {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
            let userType = result.data.name;
            if (userType == "主卡密用户") {
                if (!cardId) {
                    return {
                        code: 400,
                        msg: "副卡密ID不允许为空",
                        data: null
                    };
                }
                // 调用数据层
                result = await managerDao.getSubCardInfo(uid, cardId);
                if (result.length) {
                    result = { code: 200, msg: "获取成功", data: result[0] };
                } else {
                    result = { code: 500, msg: "未查询到该卡密", data: null };
                }
            } else if (userType == '副卡密用户') {
                // 调用数据层
                result = await managerDao.getSubCardInfoById(uid);
                if (result.length) {
                    result = { code: 200, msg: "获取成功", data: result[0] };
                } else {
                    result = { code: 500, msg: "未查询到该卡密", data: null };
                }
            } else {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 获取当前副卡密下的所有用户卡密信息
    async getAllUserCard(uid, userTypeId) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllUserCard(uid);
            result = { code: 200, msg: "获取成功", data: result };
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 分页获取当前副卡密下的所有用户卡密信息
    async getAllUserCardByPage(uid, userTypeId, page, size) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllUserCardByPage(uid, page, size);
            result = { code: 200, msg: "获取成功", data: result };
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 懒加载获取当前副卡密下的所有用户卡密信息
    async getAllUserCardLazyLoad(uid, userTypeId, cursor, limit) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllUserCardLazyLoad(uid, cursor, limit);
            if (result.length > limit) {
                let nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor } };
            } else {
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor: null } };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 搜索副卡密
    async searchSubCard(uid, userTypeId, card) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看副卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.searchSubCard(uid, card);
            if (result.length) {
                result = { code: 200, msg: "搜索成功", data: result[0] };
            } else {
                result = { code: 200, msg: "搜索结果为空", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 搜索用户卡密
    async searchUserCard(uid, userTypeId, card) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 获取用户类型
            result = await powerService.getUserType(userTypeId);
            if (!result.data) {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
            let userType = result.data.name;
            // 调用数据层
            result = await managerDao.searchUserCard(uid, userType, card);
            if (result.length) {
                result = { code: 200, msg: "搜索成功", data: result[0] };
            } else {
                result = { code: 200, msg: "搜索结果为空", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 查看用户卡密详情
    async getUserCardInfo(uid, userTypeId, id) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 获取用户类型
            result = await powerService.getUserType(userTypeId);
            if (!result.data) {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
            let userType = result.data.name;
            // 调用数据层
            result = await managerDao.getUserCardInfo(uid, userType, id);
            if (result.length) {
                result = { code: 200, msg: "获取成功", data: result[0] };
            } else {
                result = { code: 500, msg: "未查询到该卡密", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 懒加载获取某个副卡密下的用户卡密列表
    async getAllUserCardBySubCardLazyLoad(uid, userTypeId, cardId, cursor, limit) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "查看用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 获取用户类型
            result = await powerService.getUserType(userTypeId);
            if (!result.data) {
                return { code: 403, msg: "疑似令牌造假，已记录到系统日志", data: null };
            }
            let userType = result.data.name;
            if (userType != "主卡密用户") {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getAllUserCardBySubCardLazyLoad(cardId, cursor, limit);
            if (result.length > limit) {
                let nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor } };
            } else {
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor: null } };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 懒加载待审核用户卡密列表
    async getUnenableUserCardLazyLoad(uid, userTypeId, cursor, limit) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "审核用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.getUnenableUserCardLazyLoad(cursor, limit);
            if (result.length > limit) {
                let nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor } };
            } else {
                result = { code: 200, msg: "获取成功", data: { items: result, nextCursor: null } };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 授权用户卡密
    async enableUserCard(uid, userTypeId, userCardId) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "审核用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.enableUserCard(uid, userCardId);
            if (result.changedRows > 0) {
                result = { code: 200, msg: "授权成功", data: null };
            } else {
                result = { code: 200, msg: "数据无变化", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    },
    // 删除用户卡密
    async delUserCard(uid, userTypeId, cardId) {
        let result;
        try {
            // 鉴权
            result = await powerService.checkPower(userTypeId, "删除用户卡密");
            if (!result.data) {
                return { code: 403, msg: "无权限进行此操作", data: null };
            }
            // 调用数据层
            result = await managerDao.delUserCard(uid, cardId);
            if (result.changedRows > 0) {
                result = { code: 200, msg: "删除成功", data: null };
            } else {
                result = { code: 500, msg: "删除失败", data: null };
            }
        } catch (err) {
            result = { code: 500, msg: "服务器错误", data: null };
        }
        return result;
    }
};

module.exports = managerService;
